#include "key.h"
#include "flexible_button.h"
#include "ti_msp_dl_config.h"

flex_button_t user_button[USER_BUTTON_MAX];
uint8_t btn_1,btn_2,btn_3;


key_status_t key_scan(void)
{
	key_status_t states;
	states.key_1 = DL_GPIO_readPins(KEY_Group_PORT,KEY_Group_KEY_1_PIN)?1:0;
	states.key_2 = DL_GPIO_readPins(KEY_Group_PORT,KEY_Group_KEY_2_PIN)?1:0;
	states.key_3 = DL_GPIO_readPins(KEY_Group_PORT,KEY_Group_KEY_3_PIN)?1:0;
	return states;
}

static uint8_t key_1_read(void *argv)	{return key_scan().key_1;}
static uint8_t key_2_read(void *argv)	{return key_scan().key_2;}
static uint8_t key_3_read(void *argv)	{return key_scan().key_3;}

void key_1_cb(void)
{
	switch(user_button[USER_BUTTON_1].event)
	{
		case FLEX_BTN_PRESS_DOWN:
		{
			btn_1 = 1;
			break;
		}
		default:
			break;
	}
}
void key_2_cb(void)
{
	switch(user_button[USER_BUTTON_2].event)
	{
		case FLEX_BTN_PRESS_DOWN:
		{
			btn_2 = 1;
			break;
		}
		default:
			break;
	}
}
void key_3_cb(void)
{
	switch(user_button[USER_BUTTON_3].event)
	{
		case FLEX_BTN_PRESS_DOWN:
		{
			btn_3 = 1;
			break;
		}
		default:
			break;
		
	}
}

void user_button_init(void)
{
	uint8_t i = 0;
	
	user_button[USER_BUTTON_1].usr_button_read 			= key_1_read;
	user_button[USER_BUTTON_1].cb						= (flex_button_response_callback)key_1_cb;
	user_button[USER_BUTTON_2].usr_button_read 			= key_2_read;
	user_button[USER_BUTTON_2].cb						= (flex_button_response_callback)key_2_cb;
	user_button[USER_BUTTON_3].usr_button_read 			= key_3_read;
	user_button[USER_BUTTON_3].cb						= (flex_button_response_callback)key_3_cb;
	
	for (i = 0;i<USER_BUTTON_MAX;i++)
	{
		user_button[i].pressed_logic_level		= 0;
		
		user_button[i].debounce_tick = 1;
		user_button[i].short_press_start_tick	= 3;
		user_button[i].long_press_start_tick	= 36;
		user_button[i].long_hold_start_tick		= 85;
		flex_button_register(&user_button[i]);
	}	
}